﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace M3G {
    public class ShaderAppearance : AppearanceBase
    {

        #region Field
        ShaderProgram         shaderProgram;
        List<ShaderUniforms>  shaderUniforms;
        #endregion

        #region Method


        /**
         * コンストラクター
         * */
        public ShaderAppearance () {
            this.shaderProgram = null;
            this.shaderUniforms = new List<ShaderUniforms> ();
        }

        static public ShaderUniforms CreateFixedShaderUniforms () {
            var unifs = new ShaderUniforms ();

            // 固定シェーダーで使用されるUniform変数の設定
            // 以下略

            return unifs;
        }

        /**
         * コピーコンストラクター
         * */
        protected ShaderAppearance (ShaderAppearance appearance)
            : base (appearance) {
            this.shaderProgram = appearance.shaderProgram;
            this.shaderUniforms = new List<ShaderUniforms> (appearance.shaderUniforms);
        }

        public void AddShaderUniforms (ShaderUniforms uniforms) {
            if (uniforms == null) {
                throw new ArgumentNullException ("Uniforms is null");
            }
            this.shaderUniforms.Add (uniforms);
        }

        public ShaderProgram ShaderProgram {
            get { return shaderProgram; }
        }

        public void SetShaderProgram (ShaderProgram shaderProgram) {
            this.shaderProgram = shaderProgram;
        }

        public int ShaderUniformsCount  {
            get { return shaderUniforms.Count (); }
        }

        public ShaderUniforms[] ShaderUniforms {
            get { return shaderUniforms.ToArray(); }
        }

        public void RemoveShaderUniforms () {
            shaderUniforms.Clear ();
        }

        public override Object3D[] References {
            get {
                var q = new List<Object3D> ();
                q.AddRange (base.References);
                q.Add (shaderProgram);
                q.AddRange (shaderUniforms);
                q.RemoveAll (x => x == null);
                return q.ToArray ();
            }
        }

        public override Object3D Duplicate () {
            return new ShaderAppearance (this);
        }

        #endregion

    }
}
